import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;



/*@Sangeeta
 *@Uses: Program to create a table "bug_svn_m_files" to hold all the files and their path information for calculation
 *of accuracy.
 * */
public class a_bug_svn_m_file 
{
	public String passwd="123";
	public String userid="root";
	public String dbname="2apache";
	Connection conn =  null;
	
  public void process(Connection conn)
  {
	  Statement stmt =  null;
	  
	  String q = "select bugid,svn_id from a_bug_svn_info";
	  
	  
	  try 
	  {
		stmt =  conn.createStatement();
		stmt.execute(q);
		ResultSet rs =  stmt.getResultSet();
		
		while(rs.next())
		{
			ArrayList<bug_svn_m_file_obj> obj =  new ArrayList<bug_svn_m_file_obj>(); 
			
			String bugid = rs.getString("bugid");
			String svnids = rs.getString("svn_id");			
			String svn_arr[] = svnids.split(",");
			
			Statement s2 =  conn.createStatement();
			for(int i=0;i<svn_arr.length;i++)
			{
				String svn_id=svn_arr[i];
				
				String svn_q = "select * from a_svn_info where svnid='"+svn_id+"'  and file_stat='M' and fname like '%.java%'" ;
				System.out.println(svn_q);
				
				s2.execute(svn_q);
				ResultSet r2 =  s2.getResultSet();
				while(r2.next())
				{
					
					int file_no = r2.getInt("file_no");
					String file_stat= r2.getString("file_stat");
					String file_name=r2.getString("fname");
					String file_path=r2.getString("file_changed");
					
					insert_to_a_bug_svn_m_file(bugid,svn_id,file_no,file_stat,file_name,file_path,conn);
					 
				}//wh
				
				if(r2!=null)
					r2.close();	
			}//for
			
			
			if(s2!=null)
				s2.close();
		}//while
		
		if(rs!=null)
			rs.close();
		if(stmt!=null)
			stmt.close();
	  } catch (SQLException e)
	  { e.printStackTrace(); }
	  
	  
  }//preocess
		
private void insert_to_a_bug_svn_m_file(String bugid, String svn_id, int file_no, String file_stat, String file_name, 
		                               String file_path,Connection conn) 
{
	
 Statement stmt =  null;
 String insert_q = "insert into a_bug_svn_m_file values('"+bugid+"','"+svn_id+"',"+file_no+",'"+file_stat+"','"+
                   file_name+"','"+file_path+"')";
 try 
 {
	stmt =  conn.createStatement();
	stmt.executeUpdate(insert_q);
	
	if(stmt!=null)
		stmt.close();
  } catch (SQLException e) 
  { e.printStackTrace(); }
	
}

public static void main(String args[])
   {
	   db_conn db= new db_conn();
	   a_bug_svn_m_file bsmf =  new a_bug_svn_m_file();
	   
	   bsmf.conn =db.connect(bsmf.dbname, bsmf.userid, bsmf.passwd);
	   bsmf.process(bsmf.conn);
	   
	   db.close(bsmf.conn);
	   
	   
   }//main
	
}//class
